import moment from 'moment'
import Cookies from 'js-cookie'
import echarts from 'echarts'

/*
t: 时间戳、 ISO 8601格式的日期字符串
f: 格式 YYYY-MM-DD HH:mm:ss
*/
export function formatDate(t, f = 'YYYY-MM-DD HH:mm:ss') {
  const date = moment(t)

  return date.isValid() ? date.format(f) : '-'
}

/**
 * 传入参数是秒
 * 返回 秒或者分
 */
export function formatTime(t) {
  if (t > 60) {
    return moment.duration(t, 'seconds').asMinutes() + ' 分'
  } else {
    return moment.duration(t, 'seconds').asSeconds() + ' 秒'
  }
}

export const getExplorer = () => {
  const ua = window.navigator.userAgent
  const isExplorer = (exp) => {
    return ua.indexOf(exp) > -1
  }
  if (!!window.ActiveXObject || 'ActiveXObject' in window) return 'IE'
  else if (isExplorer('Firefox')) return 'Firefox'
  else if (isExplorer('Chrome')) return 'Chrome'
  else if (isExplorer('Opera')) return 'Opera'
  else if (isExplorer('Safari')) return 'Safari'
}

export const isIE = () => {
  return getExplorer() === 'IE'
}

export const isArray = (obj) => {
  return Object.prototype.toString.apply(obj) === '[object Array]'
}

export const isObject = (obj) => {
  return Object.prototype.toString.apply(obj) === '[object Object]'
}

/**
 * @description 绑定事件 on(element, event, handler)
 */
export const on = (function() {
  if (document.addEventListener) {
    return function(element, event, handler) {
      if (element && event && handler) {
        element.addEventListener(event, handler, false)
      }
    }
  } else {
    return function(element, event, handler) {
      if (element && event && handler) {
        element.attachEvent('on' + event, handler)
      }
    }
  }
})()

/**
 * @description 解绑事件 off(element, event, handler)
 */
export const off = (function() {
  if (document.removeEventListener) {
    return function(element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false)
      }
    }
  } else {
    return function(element, event, handler) {
      if (element && event) {
        element.detachEvent('on' + event, handler)
      }
    }
  }
})()

export const clone = (origin) => {
  return JSON.parse(JSON.stringify(origin))
}

export function getTheme() {
  return Cookies.get('activeTheme') ? Cookies.get('activeTheme') : 'theme-default'
}

export function setTheme(theme) {
  Cookies.set('activeTheme', theme)
  toggleClass(document.body, theme)
}

export function getActiveMenu() {
  return Cookies.get('activeTopMenu') ? Cookies.get('activeTopMenu') : ''
}

export function setActiveMenu(topmenu) {
  Cookies.set('activeTopMenu', topmenu)
}

export function toggleClass(element, className) {
  if (!element || !className) {
    return
  }
  const echartsThemeModule = require('../styles/' + className + '/echarts-theme.js')
  echarts.registerTheme('echartsTheme', echartsThemeModule.theme)
  element.className = className
}

/**
 * @description 获取月的开始时间
 * @param {Number} month 月份
 */
export function getStartMonth(year = new Date().getFullYear(), month = new Date().getMonth()) {
  const result = new Date(year, month, 1)
  result.setHours(0)
  result.setMinutes(0)
  result.setSeconds(0)
  return result
}

/**
 *  @description 获取月的结束时间
 * @param {Number} month
 */
export function getEndMonth(year = new Date().getFullYear(), month = new Date().getMonth() + 1) {
  const result = new Date(year, month, 0)
  result.setHours(23)
  result.setMinutes(59)
  result.setSeconds(59)
  return result
}

/**
 *
 * @description 几月前后
 */
export function addMonths(v, n) {
  if (v) {
    var t = new Date(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds(), v.getMilliseconds())
    t.setMonth(v.getMonth() + n)
    if (t.getDate() != v.getDate()) { t.setDate(0) }
    return t
  }
}

/**
 *
 * @description 几日前后
 */
export function addDays(v, n) {
  if (v) {
    v = new Date(v.valueOf())
    v.setDate(v.getDate() + n)
    return v
  }
}
